package org.utils.print;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.PrintRequestAttributeSet;

import org.apache.log4j.Logger;

/** A class to handle PrintJob Request
 * @author Manuel EVENO
 */
public class PrintManager
{
	protected Logger logger = null;
	/**
	 * Constructor for PrintManager.
	 */
	public PrintManager()
	{
		super();
		logger = Logger.getLogger(getClass());
	}

	/** Method print.
     * @param doc The doc to print
     * @param printAttrSet The print properties
     */
	public void print(Doc doc, PrintRequestAttributeSet printAttrSet)
	{
		// Locates default printer to print the specified document format
		// with the specified attribute set
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();
		logger.info(service.toString());
		for (int i = 0; i < service.getSupportedDocFlavors().length; i++)
		{
			DocFlavor flavor = service.getSupportedDocFlavors()[i];
			logger.debug(flavor.toString());
		}
		if (service != null)
		{
			try
			{
				DocPrintJob printJob = service.createPrintJob();
				printJob.print(null, printAttrSet);
			}
			catch (PrintException pe)
			{
				logger.error(pe.getMessage(), pe);
			}
		}
		else
		{
			logger.error("No PrintService available");
		}
	}
}
